<?php

require_once '../../includes/include_admin.php';
require_once '../../classes/az_Options.php';

run_action();

/*
 * Funciton run action
 */

function run_action() {
    if (isset($_POST['act']) && $_POST['act'] != null) {
        $action = $_POST['act'];
    } else if (isset($_GET['act']) && $_GET['act'] != null) {
        $action = $_GET['act'];
    } else {
        $action = 0;
    }

    $html = null;
    switch ($action) {
        case 1:
            $result = save_menu_config();
            if ($result) {
                $html['stt'] = true;
                $html['option'] = get_all_menu();
            }
            break;
        case 2:
            $result = update_menu();
            if($result){
                $html['stt'] = true;
                $html['option'] = get_all_menu();
            }
            break;
        case 3:
            $menu_id = $_GET['idmenu'];
            if (isset($menu_id) && is_numeric($menu_id)) {
                $html['stt'] = true;
                $html['menu'] = get_menu($menu_id);
            } else {
                $html['menu'] = '<b>Sorry server is too busy please try again!</b>';
            }
            break;
        default:
            break;
    }

    /* Send as JSON */
    header("Content-Type: application/json", true);
    echo json_encode($html);
    exit();
}

/*
 *  Function save menu 
 */

function save_menu_config() {
    $menuname = $_POST['name'];
    $jsonData = $_POST['json'];
    $htmlmenu = $_POST['html'];

    $option = new Az_Options();
    $option->create_option(null, $menuname, 'menu', $jsonData, $htmlmenu, 1);
    return $option->add_option();
}

/*
 * Function get all menu when ajax call sucess when update menu
 */

function get_all_menu() {
    global $az_db;
    $sql_P = "SELECT `id`,`option_name`,`option_type` FROM `az_option_value` WHERE `option_type`='menu' ORDER BY `id` DESC ";
    $result_P = $az_db->executeSQL_Query($sql_P);
    $option_list = '<option value="0" selected="true">--Chọn một loại menu để hiển thị--</option>';
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $option_id = $rows['id'];
            $option_name = $rows['option_name'];
            $option_list.='<option value="' . $option_id . '">' . $option_name . '</option>';
        }
        $result_P->free();
        return $option_list;
    }
    $result_P->free();
    return $option_list;
}

/*
 *  Function get one menu
 */

function get_menu($option_id) {
    global $az_db;
    $sql_P = "SELECT `description` FROM `az_option_value` WHERE `id`=" . $option_id . " ORDER BY `id` DESC ";
    $result_P = $az_db->executeSQL_Query($sql_P);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $menudes = $rows['description'];
        }
        $result_P->free();
        return $menudes;
    }
    $result_P->free();
    return '<b>Sorry server is too busy please try again!</b>';
}

/*
 * Update menu
 */

function update_menu() {
    $menuname = $_POST['namemenu'];
    $jsonData = $_POST['json'];
    $htmlmenu = $_POST['html'];
    $optId = $_POST['optionId'];
    
    $option = new Az_Options();
    $option->create_option($optId, $menuname, 'menu', $jsonData, $htmlmenu, 1);
    return $option->update_option();
}

?>
